/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.db.SqlManager;
import com.exception.DataException;
import com.javabeans.GrupoMateria;
/**
 * @author Administrador
 */
public class AdminGrupoMateria implements GrupoMateriaManager{
    private Connection c;

    public AdminGrupoMateria(Connection c){
        this.c = c;
    }
    
    @Override
    public void saveGrupoMaterias(List<GrupoMateria> gmaterias) throws DataException{
        PreparedStatement preSt = null;
        int i = 0;
        String query = "INSERT INTO GRUPO_MATERIA VALUES";
        try{
            for(GrupoMateria gm:gmaterias){
                query += "("+gm.getIdGestion()+","+gm.getIdGrupo();
                query += ","+gm.getIdPensum()+","+gm.getIdCarrera()+","+gm.getIdMateria()+"),";
                i++;
            }
            if(i > 0){
                i = query.lastIndexOf(",");
                query = query.substring(0, i);
            }            
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException e){
            throw new DataException(" AdminGrupoMateria -> saveGrupoMaterias(List<GrupoMateria> gmaterias) "+e.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    
    @Override
    public void eliminarPorGrupo(int idGestion,int idGrupo)throws DataException{
        PreparedStatement preSt = null;
        try{
            String query = "DELETE FROM GRUPO_MATERIA WHERE IDGESTION="+idGestion+" AND IDGRUPO="+idGrupo;
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException ex){
            throw new DataException(" AdminGrupoMateria -> eliminarPorGrupo(int idGestion,int idGrupo) "+ex.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
}
